//
//  SEGSubmitUserAtuhViewModel.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/6/5.
//

#import "SEGSubmitUserAtuhViewModel.h"
#import "SEGSubmitartificialauthManager.h"

@implementation SEGSubmitUserAtuhViewModel
#pragma mark - Init
-(instancetype)init{
    if (self = [super init]) {
        [self racInit];
    }
    return self;
}
- (void)racInit {
    
    @weakify(self);
    
    _userAuthorCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {

            @strongify(self);
            
//            if (![self checkSubmitData]){
//                [subscriber sendCompleted];
//                return nil;
//            }
            
            // 人工审核
            if (self.isManual) {
                NSString *proposerName = [self.proposerNameString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
                
                [[SEGSubmitartificialauthManager sharedManager] requestSubmitartificialauthWithHouseid:self.houseModel.houseId
                                                                                                   tel:self.proposerPhoneString
                                                                                                  name:proposerName
                                                                                              userType:self.userTypeCode
                                                                                                inTime:@""
                                                                                               outTime:@""
                                                                                                 relId:self.relId
                                                                                              finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                    
                    if (isSuccessed) {
                        [self.messageDataSubject sendNext:@{@"type":@"1",@"value":@"物业管理处已收到您的认证申请，敬请期待"}];
                        
                        //跳出申请认证界面
                        [subscriber sendNext:@1];
                        
                    } else {
                        [self.messageDataSubject sendNext:@{@"type":@"1",@"value":errorMessage}];
                    }
                    [subscriber sendCompleted];
                }];
                
            }else{
                //弹出匹配信息弹窗
                [subscriber sendNext:@2];
                
                NSString *proposerName = [NSString stringWithFormat:@"%@*",[self.proposerNameString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
                NSString *name = [self.nameString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
                
                [self.requestManager requestSubmitUserAtuhWithName:proposerName
                                                           houseId:self.houseModel.houseId
                                                             relId:self.relId
                                                           custTel:self.phoneString
                                                          custName:name
                                                          finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
                    if (isSuccessed) {
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            
                            // 认证积分行为设置
                            [[SEGAnalyseBehaviorManager sharedManager] requestIntegralGrowthWithCode:SEGIntegralCode.auth finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {}];
                            
                            [subscriber sendNext:@3];
                            [self.messageDataSubject sendNext:@{@"type":@"2"}];
                        });
                    } else {
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            
                            [subscriber sendNext:@3];
                            if (codeType.intValue == -1) {
                                [self.messageDataSubject sendNext:@{@"type":@"3"}];
                            }else {
                                [self.messageDataSubject sendNext:@{@"type":@"1",@"value":errorMessage}];
                            }
                        });
                    }
                    [subscriber sendCompleted];
                }];
            }
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
    
    _userAuthorFailCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            @strongify(self);
            
            //点击人工审核 直接掉接口
            NSString *proposerName = [self.proposerNameString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            [self.requestManager requestSubmitFailUserAtuhWithHouseId:self.houseModel.houseId
                                                                relId:self.relId
                                                                 name:proposerName
                                                             finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    [self.messageDataSubject sendNext:@{@"type":@"1",@"value":@"物业管理处已收到您的认证申请，敬请期待"}];
                    [subscriber sendNext:nil];
                } else {
                    [self.messageDataSubject sendNext:@{@"type":@"1",@"value":errorMessage}];
                }
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
    
    _freshProfileCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            //自动刷新用户信息
            [[SEGProfileManager sharedManager] requestProfilecompleted:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                //刷新用户信息
                [[NSNotificationCenter defaultCenter] postNotificationName:SEGRefreshUserInfoFinishedNotification object:nil];
                [subscriber sendNext:@(isSuccessed)];
                [subscriber sendCompleted];
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

#pragma mark - Publish Methods

#pragma mark - Private Methods
/**
 *  检测提交数据
 */
- (BOOL)checkSubmitData {
    
    if (self.buidingModel.name.length <= 0 || self.houseModel.roomNo.length <= 0) {
       [self.messageDataSubject sendNext:@{@"type":@"1",@"value":@"请选择认证房间号"}];
       return NO;
        
    }
    // 自助认证
    if (!self.isManual) {
        NSString *nameStr = [self.nameString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if (nameStr.length == 0) {
            [self.messageDataSubject sendNext:@{@"type":@"1",@"value":@"请输入业主/租赁人姓名"}];
            return NO;
        }
        if ([self.phoneString length] == 0) {
            [self.messageDataSubject sendNext:@{@"type":@"1",@"value":@"请输入业主/租赁人手机号"}];
            return NO;
        }
        if (self.phoneString.length != 11) {
            [self.messageDataSubject sendNext:@{@"type":@"1",@"value":kPhoneError}];
            return NO;
        }
    }
    
    NSString *proposerNameStr = [self.proposerNameString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (proposerNameStr.length == 0) {
        [self.messageDataSubject sendNext:@{@"type":@"1",@"value":@"请输入申请人姓名"}];
        return NO;
    }
    if (proposerNameStr.length > 5) {
        [self.messageDataSubject sendNext:@{@"type":@"1",@"value":@"申请人姓名不能超过5个字"}];
        return NO;
    }
    return YES;
}

#pragma mark - Getter
- (SEGUserHouserRequestManager *)requestManager {
    if (!_requestManager) {
        _requestManager = [[SEGUserHouserRequestManager alloc] init];
    }
    return _requestManager;
}

-(RACSubject *)messageDataSubject{
    if (!_messageDataSubject) {
        _messageDataSubject = [RACSubject subject];
    }
    return _messageDataSubject;
}
#pragma mark - Stter
@end
